<template>
  <div>


    <!--  按钮 -->
    <div>
      <el-radio-group v-model="category" @change="CategoryData">
        <el-radio-button label="1">M等级</el-radio-button>
        <el-radio-button label="2">金额等级</el-radio-button>
        <el-radio-button label="3">风险等级</el-radio-button>
      </el-radio-group>
    </div>


    <!-- 列表展示页面 -->
    <el-table  :header-cell-style="{background:'LightYellow	',color:'black'}"  :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))" style="width: 100%">

      <!-- <el-table-column label="编号" prop="id">
      </el-table-column> -->
      <el-table-column label="参数名称" prop="pname">
      </el-table-column>

      <el-table-column label="最小值" prop="start">
      </el-table-column>

      <el-table-column label="最大值" prop="end">
      </el-table-column>
      <el-table-column label="风险等级" prop="risk" v-if="category=='3'">
      </el-table-column>
      <el-table-column label="操作" align="center">

        <template slot-scope="scope">
          <el-button size="mini" @click="handleSaveOrUpdate(scope.$index, scope.row)">编辑</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增按钮-->
    <el-button type="primary" @click="handleSave()">新增</el-button>



    <!-- 添加 修改  M等级对话框 -->
    <el-dialog title="添加参数配置" :visible.sync="MInsertDialog" width="30%">
      <el-form ref="form" :model="MSaveForm" label-width="80px">
        <el-form-item label="参数名称">
          <el-input v-model="MSaveForm.pname"></el-input>
        </el-form-item>

        <el-form-item label="最小值">
          <el-input v-model="MSaveForm.start"></el-input>
        </el-form-item>

        <el-form-item label="最大值">
          <el-input v-model="MSaveForm.end"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="MonSubmitSave">保存</el-button>
          <el-button type="primary" @click="MInsertDialog = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加  修改 额度   对话框 -->
    <el-dialog title="添加参数配置" :visible.sync="EInsertDialog" width="30%">
      <el-form ref="form" :model="ESaveForm" label-width="80px">
        <el-form-item label="额度等级">
          <el-input v-model="ESaveForm.pname"></el-input>
        </el-form-item>

        <el-form-item label="最小值">
          <el-input v-model="ESaveForm.start"></el-input>
        </el-form-item>

        <el-form-item label="最大值">
          <el-input v-model="ESaveForm.end"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="EonSubmitSave">保存</el-button>
          <el-button type="primary" @click="EInsertDialog = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>


    <!-- 添加  修改 风险等级 对话框 -->
    <el-dialog title="修改风险参数" :visible.sync="RiskDialog" width="30%">
      <el-form ref="form" :model="RiskForm" label-width="80px">

        <el-form-item label="最小值">
          <el-input v-model="RiskForm.start"></el-input>
        </el-form-item>

        <el-form-item label="最大值">
          <el-input v-model="RiskForm.end"></el-input>
        </el-form-item>

        <el-form-item label="风险名称">
          <el-select v-model="RiskForm.pname" placeholder="请选择风险名称">
            <el-option label="高风险" value="高风险"></el-option>
            <el-option label="中风险" value="中风险"></el-option>
            <el-option label="低风险" value="低风险"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="M等级">
          <el-select v-model="RiskForm.pid" size="small" placeholder="请选择M等级">
            <el-option v-for="item in mList" :key="item.pid" :label="item.pname" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="RiskFormSubmitSave">保存</el-button>
          <el-button type="primary" @click="RiskDialog = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  import {
    cmcUrl
  } from '../../../static/baseUrl';
  import {
    Message
  } from "element-ui";
  export default {
    data() {
      return {
        mList: [],
        search: '',
        ESaveForm: {},
        RiskForm: {},
        MSaveForm: {},
        RiskDialog: false,
        SaveForm: {},
        InsertDialog: false,
        dialogFormVisible: false,
        category: '1',
        MInsertDialog: false,
        tableData: [],
        EInsertDialog: false,
      }
    },
    methods: {
      //  风险 等级的提交保存
      RiskFormSubmitSave() {
        /* category==3 */
        this.$set(this.RiskForm, "category", this.category);
        console.log("category", this.category);
        console.log("this.RiskForm", this.RiskForm);
        this.$axios({
            method: "post",
            url: `/CMC/level/add/update`,
            data: this.RiskForm,
          })
          .then((res) => {
            if (res.code == 200) {
              this.$notify({
                title: '成功',
                message: '这是一条成功的提示消息',
                type: 'success'
              });
              this.RiskDialog = false;
              this.CategoryData();
            } else {
              Message.warning(res.message);
            }
          })
          .catch((err) => {});
      },
      /* 新增  修改  M等级*/
      MonSubmitSave() {
        this.$set(this.MSaveForm, "category", this.category);
        this.$axios({
            method: "post",
            url: `/CMC/level/add/update`,
            data: this.MSaveForm,
          })
          .then((res) => {
            if (res.code == 200) {
              this.MInsertDialog = false;
              this.CategoryData();
            } else {
              Message.warning(res.message);
            }
          })
          .catch((err) => {});
      },

      /* 新增  修改  额度等级 */
      EonSubmitSave() {
        this.$set(this.ESaveForm, "category", this.category);
        this.$axios({
            method: "post",
            url: `/CMC/level/add/update`,
            data: this.ESaveForm,
          })
          .then((res) => {
            if (res.code == 200) {
              this.EInsertDialog = false;
              this.CategoryData();
            } else {
              Message.warning(res.message);
            }
          })
          .catch((err) => {});
      },


      //新增参数  配置
      handleSave() {

        if (this.category == '1') {
          this.MInsertDialog = true;
        } else if (this.category == '2') {
          this.EInsertDialog = true;
        } else if (this.category == '3') {
          this.RiskDialog = true;
        }

      },

      /* 数据  回显*/
      handleSaveOrUpdate(index, row) {
        if (this.category == '1') {
          this.MInsertDialog = true;
          this.MSaveForm = row;
        } else if (this.category == '2') {
          this.EInsertDialog = true;
          this.ESaveForm = row;
        } else if (this.category == '3') {
          // 打开对话框
          this.RiskDialog = true;
          //数据回显
          this.RiskForm = JSON.parse(JSON.stringify(row));
          console.log(this.RiskForm, "RiskForm");
          this.RiskForm.pid = row.pname;
          this.RiskForm.pname = row.risk;
          // 遍历 数组
          Object.keys(this.mList).forEach((item) => {
            let aa = this.mList[item];
            console.log("aa", aa);
            if (aa.pname == row.pname) {
              this.RiskForm.pid = aa.id;
            }
            console.log("pid====",this.RiskForm.pid);
            console.log("id====",aa.id);
          });

        }
      },

      initData() {
        this.$axios({
            method: "get",
            url: `/CMC/level/list`,
            data: "",
          })
          .then((res) => {
            if (res.code == 200) {
              this.tableData = res.data;
            } else {
              Message.warning(res.message);
            }
          })
          .catch((err) => {});
      },

      CategoryData() {
        let url = "";
        if (this.category == '1' || this.category == '2') {
          /*  有值得 话 就走 list接口*/
          url = `/CMC/level/listCategory?category=` + this.category;
        } else {
          url = `/CMC/level/list?category=` + this.category;
        }
        this.$axios({
            method: "get",
            url: url,
            data: "",
          })
          .then((res) => {
            if (res.code == 200) {
              if (this.category == '1') {
                this.mList = res.data;
              }
              this.tableData = res.data;
            } else {
              Message.warning(res.message);
            }
          })
          .catch((err) => {});
      },

    },
    created() {
      /* this.initData(); */
      this.CategoryData();
    }
  }
</script>
